package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.adapter.VideoPlayerSetAdapter;
import com.hzdesk.media.koreabatchmatvector.bean.DcfBean;
import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFile;
import com.hzdesk.media.koreabatchmatvector.bean.XmlUserBean;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.logic.contract.VideoPlayerContract;
import com.hzdesk.media.koreabatchmatvector.logic.model.VideoPlayerModle;
import com.hzdesk.media.koreabatchmatvector.utils.FileUtil;
import com.hzdesk.media.koreabatchmatvector.utils.SharedPreferencesUtils;
import com.hzdesk.media.koreabatchmatvector.utils.XMLUtil;
import com.universalvideoview.UniversalMediaController;
import com.universalvideoview.UniversalVideoView;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2018/6/23.
 * 杭州席媒科技有限公司  Developers  刘传华  09
 * 佛祖镇楼 bug辟易
 */

public class VideoPlayerPersenter implements VideoPlayerContract.IVideoPlayerPersenter {
    private String TAG = VideoPlayerPersenter.class.getSimpleName();
    private Context mContext;
    private VideoPlayerContract.IVideoPlayerView iVideoPlayerView;
    private VideoPlayerModle iVideoPlayerModle;
    private boolean isShow = false;
    private UniversalVideoView mVideoView;
    private UniversalMediaController mController;
    private VideoFile mVideoFile;
    private DcfBean bean;
    private String xmlString, videoString, awrString;
    private boolean isPause = false;
    protected static final int PROGRESS = 0;
    private MediaPlayer mediaPlayer;
    private long awrDuration;
    private RecyclerView mRecyclerView;
    List<XmlUserBean> mList;
    private VideoPlayerSetAdapter videoPlayerSetAdapter;
    private boolean isShowAllUsetAnnotation=true;//显示所有用户批注


    public VideoPlayerPersenter(VideoPlayerContract.IVideoPlayerView mVideoPlayerView) {
        this.iVideoPlayerView = mVideoPlayerView;
        iVideoPlayerModle=new VideoPlayerModle();
    }

    @Override
    public void onClickedTobBack() {
        iVideoPlayerView.finshPager();
    }

    @Override
    public void onClickedContent() {
        if (isShow) {  //todo 控制标题栏，进度栏 显示隐藏
            iVideoPlayerView.hide();
            isShow = false;
        } else {
            iVideoPlayerView.show();
            isShow = true;
        }
    }

    @Override
    public void onClickeTopCanvasSwitch() {


    }

    @Override
    public void onStartDraw(Context context, VideoFile videoFile, UniversalVideoView videoView, UniversalMediaController controller) {
        mContext=context;
        mVideoFile = videoFile;
        mVideoView = videoView;
        mController = controller;
        mediaPlayer = new MediaPlayer();
        mVideoView.setMediaController(mController);
        getFile();
        readXml();
        //画布准备工作
        iVideoPlayerView.startDraw(bean, mVideoFile.getSecondname());

        if (videoString != null) {
            mVideoView.setVideoPath(mVideoFile.getPath() + "/" + mVideoFile.getName() + AppConfig.FILE_TYPE_MP4);
        }
        if (awrString != null) {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(awrString);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //设置画布的状态 展示或者隐藏
        mController.setCanvasState(SharedPreferencesUtils.isShowCanvas(context));

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                handler.sendEmptyMessage(0);
                mVideoView.start();
                mediaPlayer.start();
                mController.setTitle(videoString);
                iVideoPlayerView.onPrepared();
            }
        });
        mVideoView.setVideoViewCallback(new UniversalVideoView.VideoViewCallback() {
            @Override
            public void onScaleChange(boolean isFullscreen) {

            }

            @Override
            public void onPause(MediaPlayer mediaPlayer) {
                iVideoPlayerView.onVideoPause();

            }

            @Override
            public void onStart(MediaPlayer mediaPlayer) {
                iVideoPlayerView.onVideoStart();

            }

            @Override
            public void onBufferingStart(MediaPlayer mediaPlayer) {

            }

            @Override
            public void onBufferingEnd(MediaPlayer mediaPlayer) {

            }
        });
        mVideoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mController.setCanvasListener(new UniversalMediaController.CanvasLister() {
            @Override
            public void showCanvas() {
                iVideoPlayerView.show();
            }

            @Override
            public void hideCanvas() {
                iVideoPlayerView.hide();
            }

            @Override
            public void back() {
                iVideoPlayerView.finshPager();
            }

            @Override
            public void hideControlView() {


            }

            @Override
            public void showControlView() {
                iVideoPlayerView.hideSetPage();

            }

            @Override
            public void onProgressChanged(int progress) {

                Log.d(TAG,"progress:"+progress);
                long duration=mediaPlayer.getDuration();
                long position=duration*progress;
                if (position>=0&&progress<=mediaPlayer.getDuration()){
                    mediaPlayer.seekTo((int) position);
                }

            }

        });

        mController.setSetPageLisenter(new UniversalMediaController.setPageLisenter() {
            @Override
            public void showSetPage() {
                mController.hide();
                iVideoPlayerView.showSetPage();
            }

            @Override
            public void hideSetPage() {
                iVideoPlayerView.hideSetPage();

            }
        });

    }

    @Override
    public void initRecyclerView(RecyclerView recyclerView) {
        mRecyclerView=recyclerView;
        mList=bean.getUserList();
        LinearLayoutManager layoutmanager = new LinearLayoutManager(mContext);
        layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutmanager);
        videoPlayerSetAdapter=new VideoPlayerSetAdapter(mList,mContext);
        mRecyclerView.setAdapter(videoPlayerSetAdapter);
        videoPlayerSetAdapter.setOnSwitchChangeLisenter(new VideoPlayerSetAdapter.OnSwitchChangeLisenter() {
            @Override
            public void show(int num) {//显示用户批注
                iVideoPlayerView.showUserAnnotation(num);

            }

            @Override
            public void hide(int num) {//隐藏用户批注
                iVideoPlayerView.hideUserAnnotation(num);

            }

            @Override
            public void allShow() {//显示所有用户批注
                iVideoPlayerView.showAllUserAnnotation();

            }

            @Override
            public void allhide() {//隐藏所有用户批注
                iVideoPlayerView.hideAllUserAnnotation();

            }
        });


    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    long currentPosition = mVideoView.getCurrentPosition();
                    if (mVideoView.isPlaying()) {
                        handler.sendEmptyMessageDelayed(0, 1);
                        iVideoPlayerView.onChangeProgress(currentPosition);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    //读取批注文件
    public void readXml() {
        if (!xmlString.equals(AppConfig.NULL_STRING)) {
            //File file = new File(mCurrentVideoFile.getPath() + "/" + mCurrentVideoFile.getName(), xmlString);
            bean = XMLUtil.readXMLFile(new File(mVideoFile.getPath() + "/" + mVideoFile.getName() + AppConfig.FILE_TYPE_XML));
        }
    }

    //初始化文件
    private void getFile() {
        File file = new File(mVideoFile.getPath());
        //Log.d(TAG, filename);
        List<File> fileList = FileUtil.getAllFiles(file);
        for (int i = 0; i < fileList.size(); i++) {
            if (fileList.get(i).getName().contains(AppConfig.FILE_TYPE_XML)) {
                xmlString = fileList.get(i).getName();
                Log.d(TAG, xmlString);
            }
            if (fileList.get(i).getName().contains(AppConfig.FILE_TYPE_MP4)) {
                videoString = fileList.get(i).getName();
                Log.d(TAG, videoString);
            }
            if (fileList.get(i).getName().contains(AppConfig.FILE_TYPE_AMR)) {
                awrString = fileList.get(i).getName();
                Log.d(TAG, awrString);
            }
        }

    }

}
